<?php

namespace Admin\Models\track;

use Admin\Models\BaseModel;

class XhtrackTask extends BaseModel
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=11, nullable=false)
     */
    public $iTTId;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sTaskName;

    /**
     * @var string
     * @Column(type="string", length=32, nullable=true)
     */
    public $sFeedbackId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $sTaskContent;

    /**
     *
     * @var integer
     * @Column(type="integer", length=2, nullable=true)
     */
    public $iTaskType;

    /**
     *
     * @var integer
     * @Column(type="integer", length=2, nullable=true)
     */
    public $iPriority;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tOfferTime;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tPlanFinishTime;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tRealFinishTime;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sSchoolId;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sConfirmId;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sOfferIds;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sOfferUserName;

    /**
     *
     * @var integer
     * @Column(type="integer", length=2, nullable=true)
     */
    public $iOfferUserType;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mRemarks;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iState;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iSupportState;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iConfirmState;

    /**
     *
     * @var string
     * @Column(type="string", length=100, nullable=true)
     */
    public $sAppName;


    /**
     *
     * @var string
     * @Column(type="string", length=50, nullable=true)
     */
    public $sAppPackage;

    /**
     *
     * @var string
     * @Column(type="string", length=50, nullable=true)
     */
    public $sSolveVersion;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mStateLog;

    /**
     *
     * @var integer
     * @Column(type="integer", length=1, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iCreateUserId;


    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     *
     * @var int
     * @Column(type="int",length=10, nullable=true)
     */
    public $iProductId;

    /**
     *
     * @var string
     * @Column(type="string",length=200, nullable=true)
     */
    public $iProblemStyle;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iReportUserId;

    /**
     *
     * @var integer
     * @Column(type="integer", length=1, nullable=true)
     */
    public $iProLevel;

    /**
     *
     * @var integer
     * @Column(type="decimal", length=10, nullable=true)
     */
    public $dHandlingTime;

    /**
     *
     * @var integer
     * @Column(type="integer", length=10, nullable=true)
     */
    public $iInfluenceNum;

    /**
     *
     * @var string
     * @Column(type="string", length=1, nullable=true)
     */
    public $sAppVersion;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sReason;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sSolveWay;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sVersion;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $sReplyContent;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHTrack_Task';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhtrackTask[]|XhtrackTask
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhtrackTask
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 数据预处理
     *
     * @return bool
     *
     * create by ck 20170511
     */
    public function afterFetch()
    {
        //学校确认Id转化为数组
        $this->sConfirmId = $this->sConfirmId ? explode(',', trim($this->sConfirmId, ',')) : [];
        $confirmSchool = [];
        foreach ($this->sConfirmId as $info) {
            $info = explode(':', $info);
            $confirmSchool[$info[0]] = isset($info[1]) ? $info[1] : '';
        }
        $this->sConfirmId = $confirmSchool;

        //日志处理
        $this->mStateLog and $this->mStateLog = json_decode(base64_decode($this->mStateLog), true);
        return true;
    }

    /**
     * 保存前处理数据
     *
     * @return bool
     *
     * create by ck 20170511
     */
    public function beforeSave()
    {
        //学校确认Id转化为字符串
        $confirmSchool = [];
        foreach((array)$this->sConfirmId as $schId => $state){
            $confirmSchool[] = $schId . ':' . $state;
        }
        $this->sConfirmId = implode(',', $confirmSchool);

        //日志处理
        $this->mStateLog and $this->mStateLog = base64_encode(json_encode($this->mStateLog, JSON_UNESCAPED_UNICODE));
        return true;
    }

    /**
     * 创建/修改model
     *
     * @param $data
     * @param $obj
     * @return XhtrackTask
     *
     * create by ck 20170314
     * modify by ck 20170330
     */
    public static function createRecord($data, $obj = null)
    {
        if(!$obj){
            $obj = $data['iTTId'] ? self::findFirst($data['iTTId']) : new self;
            $obj or $obj = new self;
        }
        unset($data['iTTId']);
        $obj->assign($data);
        $obj->tOfferTime or $obj->tOfferTime = null;
        $obj->tPlanFinishTime or $obj->tPlanFinishTime = null;
        $obj->tRealFinishTime or $obj->tRealFinishTime = null;
//        $obj->tOfferTime = $obj->tOfferTime ? date('Y-m-d H:i:s', strtotime($obj->tOfferTime)) : null;
//        $obj->tPlanFinishTime = $obj->tPlanFinishTime ? date('Y-m-d H:i:s', strtotime($obj->tPlanFinishTime)) : null;
//        $obj->tRealFinishTime = $obj->tRealFinishTime ? date('Y-m-d H:i:s', strtotime($obj->tRealFinishTime)) : null;
        return $obj;
    }

    /**
     * 根据ID删除记录
     *
     * @param $idArr
     * @return bool
     *
     * create by ck 20170401
     */
    public static function deleteRecordById($idArr)
    {
        return self::find([
            'conditions' => 'bDelete = 0 AND iTTId IN ({idArr:array})',
            'bind'       => ['idArr' => $idArr]
        ])->delete();
    }



}
